package com.payment.ktb.activity.main4;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.flyco.animation.BaseAnimatorSet;
import com.flyco.animation.BounceEnter.BounceTopEnter;
import com.flyco.animation.FlipEnter.FlipVerticalSwingEnter;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.flyco.dialog.widget.base.BottomBaseDialog;
import com.flyco.dialog.widget.base.TopBaseDialog;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.activity.ShowH5WithTitleActivity;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.PhtotUtils;
import com.payment.ktb.utils.SharedPreferencesUtils;
import com.payment.ktb.utils.ToastUtils;
import com.squareup.picasso.Picasso;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.provider.UserDictionary.Words.APP_ID;

/**
 * Created by admin on 2017/9/4.
 */

public class InviteCodeActivity extends BaseActivity {

    private BaseAnimatorSet mBasIn;
    @BindView(R.id.tv_invitecode_code)
    TextView tv_invitecode_code;
    @BindView(R.id.iv_invitecode_qrcode)
    ImageView iv_invitecode_qrcode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invitecode);
        ButterKnife.bind(this);
        mBasIn = new BounceTopEnter();
        initData();
    }


    private void initData() {
        tv_invitecode_code.setText("邀请码 " + SharedPreferencesUtils.getString("loginName",""));
        if (!TextUtils.isEmpty(ConstantsUser.picQr)) {
            Picasso.with(mContext).load(ConstantsUser.picQr).fit().into(iv_invitecode_qrcode);
        }
    }


    @OnClick({R.id.rl_invitecode_left,R.id.rl_invitecode_right,R.id.iv_invitecode_wechat,R.id.iv_invitecode_wechat_friendcircle,R.id.iv_invitecode_webo})
    public void clickEvent(View view){

        UMImage image = null;
        UMWeb web = null;

        switch (view.getId()){

            //返回
            case R.id.rl_invitecode_left:
                finish();
                break;

            //邀请记录
            case R.id.rl_invitecode_right:
                getInviteInfo();
                break;

            //微信朋友
            case R.id.iv_invitecode_wechat:

                image = new UMImage(InviteCodeActivity.this,  R.mipmap.ktblogo);//网络图片
                web = new UMWeb(SharedPreferencesUtils.getString(ConstantsUser.inviteUrl));
                web.setTitle("臻享指尖便利，还看捷牛旅行");//标题
                web.setThumb(image);  //缩略图
                web.setDescription("捷牛旅行APP是一款提供金融、小额贷款、保险等服务的综合类生活服务平台，安全、便捷，值得信赖。");//描述

                new ShareAction(InviteCodeActivity.this)
                        .setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                        .withMedia(web)
                        .share();


                break;

            //微信朋友圈
            case R.id.iv_invitecode_wechat_friendcircle:


                image = new UMImage(InviteCodeActivity.this,  R.mipmap.ktblogo);//网络图片
                web = new UMWeb(SharedPreferencesUtils.getString(ConstantsUser.inviteUrl));
                web.setTitle("臻享指尖便利，还看捷牛旅行");//标题
                web.setThumb(image);  //缩略图
                web.setDescription("捷牛旅行APP是一款提供金融、小额贷款、保险等服务的综合类生活服务平台，安全、便捷，值得信赖。");//描述

                new ShareAction(InviteCodeActivity.this)
                        .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)//传入平台
                        .withMedia(web)
                        .share();


                break;

            //微博
            case R.id.iv_invitecode_webo:

                image = new UMImage(InviteCodeActivity.this, R.mipmap.ktblogo);//网络图片
                web = new UMWeb(SharedPreferencesUtils.getString(ConstantsUser.inviteUrl));
                web.setTitle("臻享指尖便利，还看捷牛旅行");//标题
                web.setThumb(image);  //缩略图
                web.setDescription("捷牛旅行APP是一款提供金融、小额贷款、保险等服务的综合类生活服务平台，安全、便捷，值得信赖。");//描述

                new ShareAction(InviteCodeActivity.this)
                        .setPlatform(SHARE_MEDIA.SINA)//传入平台
                        .withMedia(web)
                        .share();

                break;

        }





    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }


    //用户邀请兑换信息
    public void getInviteInfo() {


        progressDialogUtils.showProgress();

        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        String timestamp = String.valueOf(new Date().getTime());
        map.put("timestamp",timestamp);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.UserInviteDetail, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    getNoChargeList(jsonObject);

                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });


    }





    public void getNoChargeList(final JSONObject InviteChargeInfoJson) {


        progressDialogUtils.showProgress();

        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        map.put("isCharge","0");

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.UserInviteList, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    Intent intent = new Intent(mContext,InviteRecordActivity.class);
                    intent.putExtra("isCharge",InviteChargeInfoJson.optInt("isCharge"));
                    intent.putExtra("residue",InviteChargeInfoJson.optInt("residue"));
                    intent.putExtra("chargeUser",InviteChargeInfoJson.optInt("chargeUser"));
                    intent.putExtra("chargeDay",InviteChargeInfoJson.optInt("chargeDay"));
                    intent.putExtra("records",jsonObject.optString("records"));
                    startActivity(intent);
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });

    }




}


